<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改药品信息')"/>
</head>
<body>
<form class="layui-form" th:object="${hospitalDruginfo}">
    <div class="mainBox">
        <div class="main-container">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="layui-form-item">
                <label class="layui-form-label">名称：</label>
                <div class="layui-input-block">
                    <input type="text" name="name" th:field="*{name}" placeholder="请输入名称" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">供应商：</label>
                <div class="layui-input-block layui-form" lay-filter="supplierData">
                    <select name="hospitalSupplier" th:field="*{hospitalSupplier}" lay-verify="" lay-search
                            th:attr="default-value=*{hospitalSupplier}">
                        <option value="">请选择</option>
                    </select>
                    <!--<input type="text" name="hospitalSupplier" th:field="*{hospitalSupplier}" placeholder="请输入供应商"
                           class="layui-input"/>-->
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">生产时间：</label>
                <div class="layui-input-block">
                    <div class="input-group">
                        <input type="text" name="producttime" class="layui-input" id="producttime"
                               th:value="*{producttime}">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">保质期：</label>
                <div class="layui-input-block">
                    <input type="text" name="warrenty" th:field="*{warrenty}" placeholder="请输入保质期" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">药品编码：</label>
                <div class="layui-input-block">
                    <input type="text" name="number" th:field="*{number}" placeholder="请输入药品编码" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">价格：</label>
                <div class="layui-input-block">
                    <input type="text" name="price" th:field="*{price}" placeholder="请输入价格" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">库存：</label>
                <div class="layui-input-block">
                    <input type="text" name="stock" th:field="*{stock}" placeholder="请输入库存" class="layui-input"/>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="druginfo-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
</body>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'laydate', 'dictionary'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;

        let prefix = "/hospital/druginfo/";

        laydate.render({
            elem: '#producttime',
            type: 'date',
            format: 'yyyy-MM-dd'
        });

        form.on('submit(druginfo-update)', function (data) {
            for (var key in data.field) {
                var type = $(data.form).find("input[name='" + key + "']").attr("type");
                if (type == "checkbox") {
                    var value = "";
                    $(data.form).find("input[name='" + key + "']:checked").each(function () {
                        value += $(this).val() + ",";
                    })
                    if (value != "") {
                        value = value.substr(0, value.length - 1);
                        data.field[key] = value;
                    }

                }
            }
            $.ajax({
                url: prefix + 'update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.layui.table.reload("druginfo-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
        function getSupplierList(){
            $.ajax({
                url: '/hospital/supplier/getSupplierList',
                type: 'get',
                success: function (result) {
                    if (result.success) {
                        $.each(result.data, function(index, item) {
                            $('#hospitalSupplier')
                                .append(new Option(item.name, item.id));
                        });
                        form.render('select','supplierData');
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            });
        }
        getSupplierList();
    });
</script>
</html>